﻿Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.String
Imports IDServer
Imports System.Text
Imports System.Collections
Imports CrystalDecisions.CrystalReports.Engine


Public Class Frm_INTPickGoodsList
    Implements IDServer.IDServer.IDServer


    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp
    Private mDataset As DataSet
    Private MasterDS As DataSet
    Private SelectNode As TreeNode
    Private Img As ImageList
    Public bFind As Boolean
    Private WithEvents MainForm As Frm_MainForm
    Private _DataTB As DataTable
    Dim Dtab As DataTable


    Public Property DataTB() As DataTable
        Get
            Return _DataTB
        End Get
        Set(ByVal value As DataTable)
            _DataTB = value
        End Set
    End Property
#Region "interface"
    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property



    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd

    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel

    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete

    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit

    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave

    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function

#End Region

    Private Sub Lod_CBStore()
        Dim sql As String
        Dim DTA As DataTable
        sql = "select Distinct b.fshortname as  tostore from Stor_TradeMaster a left join BSC_StoreHouse b on a.tostore = b.fcode where inorouttype = '03'  and TradeType = '-'"
        DTA = ReturnDataTable(sql)
        Me.CBStore.DataSource = DTA
        Me.CBStore.DisplayMember = "tostore"

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.ProgressBar1.Value = 0

        Dim mDs As DataSet

        Try

            Dim Porttable As DataTable = DirectCast(Me.Dgv_Port.DataSource, DataTable)
            Dim PortStr As Text.StringBuilder = New Text.StringBuilder
            Dim count As Int32 = 0
            If Porttable.Rows.Count > 0 Then
                For Each row As DataRow In Porttable.Rows
                    If row("Isselected") = True Then
                        count = count + 1
                        If PortStr.Length > 0 Then
                            PortStr.Append(",")
                        End If
                        PortStr.Append("'")
                        PortStr.Append(row(1))
                        PortStr.Append("'")
                    End If
                Next
            End If


            Dim Port As String = PortStr.ToString

            Dim Pathstr As String = ""
            Dim SubPathstr As String = ""
            Dim ConfigNode() As String = ReadConfigFile.Split(";")
            Dim mServer As String = ""
            Dim mDataBase As String = ""
            Dim mUserID As String = ""
            Dim mPassword As String = ""
            Dim sql As String = ""
            Dim InOrOut As String = ""
            Dim StartDate As String = ""
            Dim EndDate As String = ""
            Me.ProgressBar1.Value = 10

            'Pathstr = "D:\Reports\Store\RepInOutCount.rpt"
            Pathstr = GetReportPath("Store", "PickGoodsList.rpt")
            Me.ProgressBar1.Value = 20
            For i As Integer = 0 To ConfigNode.Length - 1
                If ConfigNode(i).Split("=")(0).ToLower = "Server".ToLower Then
                    mServer = ConfigNode(i).Split("=")(1)
                End If
                If ConfigNode(i).Split("=")(0).ToLower = "DataBase".ToLower Then
                    mDataBase = ConfigNode(i).Split("=")(1)
                End If
                If ConfigNode(i).Split("=")(0).ToLower = "Uid".ToLower Then
                    mUserID = ConfigNode(i).Split("=")(1)
                End If
                If ConfigNode(i).Split("=")(0).ToLower = "Pwd".ToLower Then
                    mPassword = ConfigNode(i).Split("=")(1)
                End If
            Next
            Me.ProgressBar1.Value = 30


            Dim myReportDoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
            Dim myParameterValues As New CrystalDecisions.Shared.ParameterValues
            Dim myDiscreteValue As New CrystalDecisions.Shared.ParameterDiscreteValue
            Dim logonInfo As New CrystalDecisions.Shared.TableLogOnInfo
            Dim table As CrystalDecisions.CrystalReports.Engine.Table
            Me.ProgressBar1.Value = 40
            myReportDoc.Load(Pathstr)
            Me.ProgressBar1.Value = 50
            For Each table In myReportDoc.Database.Tables
                logonInfo = table.LogOnInfo
                With logonInfo.ConnectionInfo
                    .ServerName = mServer
                    .DatabaseName = mDataBase
                    .UserID = mUserID
                    .Password = mPassword
                End With
                table.ApplyLogOnInfo(logonInfo)
            Next

            Me.ProgressBar1.Value = 60

            StartDate = FormatDate(Me.StartDate.Value)
            EndDate = FormatDate(Me.EndDate.Value)

            If count > 0 Then

                sql = " SELECT  B.STOREPLACE,REPLACE(B.GOODSBARCODE,'*','') AS GOODSBARCODE,C.ENNAME,SUM(B.GOODSAMOUNT) AS GOODSAMOUNT,SUM(B.GOODSAMOUNT)* isnull(PackingNum,1) as PackingNum , " & vbCrLf
                sql += " C.SUPPERITEM,ISNULL(E.FPORT,'') AS FPORT  FROM  " & vbCrLf
                sql += " DBO.STOR_TRADEMASTER A LEFT JOIN STOR_TRADEDETAIL B ON A.BONUM=B.BONUM  " & vbCrLf
                sql += " LEFT JOIN BSC_GOODS C ON B.GOODSBARCODE = C.BARCODE " & vbCrLf
                sql += " LEFT JOIN BSC_STOREHOUSE D ON A.TOSTORE = D. FCODE LEFT JOIN BSC_STORAGE E ON B.STOREPLACE = E.FSTORAGEID " & vbCrLf
                sql += " RIGHT JOIN (SELECT LISTCODE AS INOROUTTYPE FROM DBO.BSC_OTHERDEFINEB WHERE ITEMNO=(SELECT ID FROM  " & vbCrLf
                sql += " DBO.BSC_OTHERDEFINEA WHERE ITEMCODE = 'OUTSTORETYPE')AND LISTC = 'Y') F ON A.INOROUTTYPE = F.INOROUTTYPE " & vbCrLf
                sql += " WHERE TRADETYPE = '-'  " & vbCrLf

                sql += "and  Convert(varchar(10),a.DeliverDate,21) between '" & StartDate & "' and '" & EndDate & "'"
                Me.ProgressBar1.Value = 70


                If Me.CBStore.Text.Length > 0 Then
                    sql += " and d.fshortName  = '" & Me.CBStore.Text.ToString.Trim & "'"
                End If

                sql += "   and isnull(e.fport,'') in (" & Port & ")  group by e.fport, b.Storeplace,GoodsBarCode,Enname,SupperItem,PackingNum order by  b.Storeplace desc "
            Else

                sql = " SELECT  B.STOREPLACE,REPLACE(B.GOODSBARCODE,'*','') AS GOODSBARCODE,C.ENNAME,SUM(B.GOODSAMOUNT) AS GOODSAMOUNT,SUM(B.GOODSAMOUNT)* isnull(PackingNum,1) as PackingNum , " & vbCrLf
                sql += " C.SUPPERITEM,ISNULL(E.FPORT,'') AS FPORT  FROM  " & vbCrLf
                sql += " DBO.STOR_TRADEMASTER A LEFT JOIN STOR_TRADEDETAIL B ON A.BONUM=B.BONUM  " & vbCrLf
                sql += " LEFT JOIN BSC_GOODS C ON B.GOODSBARCODE = C.BARCODE " & vbCrLf
                sql += " LEFT JOIN BSC_STOREHOUSE D ON A.TOSTORE = D. FCODE LEFT JOIN BSC_STORAGE E ON B.STOREPLACE = E.FSTORAGEID " & vbCrLf
                sql += " RIGHT JOIN (SELECT LISTCODE AS INOROUTTYPE FROM DBO.BSC_OTHERDEFINEB WHERE ITEMNO=(SELECT ID FROM  " & vbCrLf
                sql += " DBO.BSC_OTHERDEFINEA WHERE ITEMCODE = 'OUTSTORETYPE')AND LISTC = 'Y') F ON A.INOROUTTYPE = F.INOROUTTYPE " & vbCrLf
                sql += " WHERE TRADETYPE = '-'  " & vbCrLf

                sql += "and  Convert(varchar(10),a.DeliverDate,21) between '" & StartDate & "' and '" & EndDate & "'"
                Me.ProgressBar1.Value = 70


                If Me.CBStore.Text.Length > 0 Then
                    sql += " and d.fshortName  = '" & Me.CBStore.Text.ToString.Trim & "'"
                End If

                sql += "  group by e.fport, b.Storeplace,GoodsBarCode,Enname,SupperItem,PackingNum order by  b.Storeplace desc "

            End If

            Me.ProgressBar1.Value = 80
            mDs = New DataSet
            mDs = ReturnDataset(sql)
            If mDs.Tables(0).Rows.Count <= 0 Then
                MessageBox.Show("沒有數據可以列印！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            End If
            Me.ProgressBar1.Value = 90
            'myReportDoc.SetParameterValue("StartDate", StartDate)
            'myReportDoc.SetParameterValue("EndDate", EndDate)

            myReportDoc.SetDataSource(mDs.Tables(0))

            CrystalReportViewer1.ReportSource = myReportDoc
            Dim TxtStartDate As CrystalDecisions.CrystalReports.Engine.TextObject = myReportDoc.ReportDefinition.ReportObjects("Text7")
            Dim TxtEndDate As CrystalDecisions.CrystalReports.Engine.TextObject = myReportDoc.ReportDefinition.ReportObjects("Text8")
            TxtStartDate.Text = Format(Me.StartDate.Value.ToString("yyyy/MM/dd"), "yyyy/MM/dd")
            TxtEndDate.Text = Format(Me.EndDate.Value.ToString("yyyy/MM/dd"), "yyyy/MM/dd")

            CrystalReportViewer1.Zoom(1)
            Me.ProgressBar1.Value = 100
        Catch ex As Exception
        Finally
            mDs = Nothing
        End Try

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim sql As String = ""
        Dim StartDate As String = ""
        Dim EndDate As String = ""
        StartDate = FormatDate(Me.StartDate.Value)
        EndDate = FormatDate(Me.EndDate.Value)
        Me.ProgressBar1.Value = 10
        Dim Porttable As DataTable = DirectCast(Me.Dgv_Port.DataSource, DataTable)
        Dim PortStr As Text.StringBuilder = New Text.StringBuilder
        Dim count As Int32 = 0
        If Porttable.Rows.Count > 0 Then
            For Each row As DataRow In Porttable.Rows
                If row("Isselected") = True Then
                    count = count + 1
                    If PortStr.Length > 0 Then
                        PortStr.Append(",")
                    End If
                    PortStr.Append("'")
                    PortStr.Append(row(1))
                    PortStr.Append("'")
                End If
            Next
        End If

        Me.ProgressBar1.Value = 20
        Dim Port As String = PortStr.ToString




        ' ''char(9)為Tab鍵，char(13)為Enter鍵
        'sql = " select  b.StorePlace,Replace(b.goodsbarcode,'*','') as GoodsBarcode,c.Enname,sum(b.GoodsAmount) as GoodsAmount,c.SupperItem from "
        'sql += " dbo.Stor_TradeMaster a left join Stor_TradeDetail b on a.bonum=b.bonum "
        'sql += "left join bsc_goods c on b.goodsbarcode = c.barcode left join BSC_StoreHouse d on a.tostore = d. fcode where inorouttype = '03'  and TradeType = '-' "
        'sql += "and  Convert(varchar(10),a.DeliverDate,21) between '" & StartDate & "' and '" & EndDate & "'"



        'If Me.CBStore.Text.Length > 0 Then
        '    sql += " and d.fshortName  = '" & Me.CBStore.Text.ToString.Trim & "'"
        'End If

        'sql += "group by b.Storeplace,GoodsBarCode,Enname,SupperItem order by  b.Storeplace desc "


        Me.ProgressBar1.Value = 30
        If count > 0 Then

            sql = " SELECT  B.STOREPLACE,REPLACE(B.GOODSBARCODE,'*','') AS GOODSBARCODE,C.ENNAME,SUM(B.GOODSAMOUNT) AS GOODSAMOUNT , " & vbCrLf
            sql += " C.SUPPERITEM,ISNULL(E.FPORT,'') AS FPORT , TOSTORE AS DEPT FROM  " & vbCrLf
            sql += " DBO.STOR_TRADEMASTER A LEFT JOIN STOR_TRADEDETAIL B ON A.BONUM=B.BONUM  " & vbCrLf
            sql += " LEFT JOIN BSC_GOODS C ON B.GOODSBARCODE = C.BARCODE " & vbCrLf
            sql += " LEFT JOIN BSC_STOREHOUSE D ON A.TOSTORE = D. FCODE LEFT JOIN BSC_STORAGE E ON B.STOREPLACE = E.FSTORAGEID " & vbCrLf
            sql += " RIGHT JOIN (SELECT LISTCODE AS INOROUTTYPE FROM DBO.BSC_OTHERDEFINEB WHERE ITEMNO=(SELECT ID FROM  " & vbCrLf
            sql += " DBO.BSC_OTHERDEFINEA WHERE ITEMCODE = 'OUTSTORETYPE')AND LISTC = 'Y') F ON A.INOROUTTYPE = F.INOROUTTYPE " & vbCrLf
            sql += " WHERE TRADETYPE = '-'  " & vbCrLf
            sql += " AND  CONVERT(VARCHAR(10),A.DELIVERDATE,21) BETWEEN '" & StartDate & "' AND'" & EndDate & "'"


            If Me.CBStore.Text.Length > 0 Then
                sql += " AND D.fshortName  = '" & Me.CBStore.Text.ToString.Trim & "'"
            End If

            sql += "   and isnull(e.fport,'') in (" & Port & ") GROUP BY E.FPORT, B.STOREPLACE,GOODSBARCODE,ENNAME,SUPPERITEM ,TOSTORE ORDER BY  B.STOREPLACE DESC  "
        Else
            sql = " SELECT  B.STOREPLACE,REPLACE(B.GOODSBARCODE,'*','') AS GOODSBARCODE,C.ENNAME,SUM(B.GOODSAMOUNT) AS GOODSAMOUNT," & vbCrLf
            sql += " C.SUPPERITEM,ISNULL(E.FPORT,'') AS FPORT , TOSTORE AS DEPT FROM  " & vbCrLf
            sql += " DBO.STOR_TRADEMASTER A LEFT JOIN STOR_TRADEDETAIL B ON A.BONUM=B.BONUM  " & vbCrLf
            sql += " LEFT JOIN BSC_GOODS C ON B.GOODSBARCODE = C.BARCODE " & vbCrLf
            sql += " LEFT JOIN BSC_STOREHOUSE D ON A.TOSTORE = D. FCODE LEFT JOIN BSC_STORAGE E ON B.STOREPLACE = E.FSTORAGEID " & vbCrLf
            sql += " RIGHT JOIN (SELECT LISTCODE AS INOROUTTYPE FROM DBO.BSC_OTHERDEFINEB WHERE ITEMNO=(SELECT ID FROM  " & vbCrLf
            sql += " DBO.BSC_OTHERDEFINEA WHERE ITEMCODE = 'OUTSTORETYPE')AND LISTC = 'Y') F ON A.INOROUTTYPE = F.INOROUTTYPE " & vbCrLf
            sql += " WHERE TRADETYPE = '-'  " & vbCrLf
            sql += " AND  CONVERT(VARCHAR(10),A.DELIVERDATE,21) BETWEEN  '" & StartDate & "' and '" & EndDate & "'"

            Me.ProgressBar1.Value = 40
            If Me.CBStore.Text.Length > 0 Then
                sql += " and d.fshortName  = '" & Me.CBStore.Text.ToString.Trim & "'"
            End If

            sql += "  GROUP BY E.FPORT, B.STOREPLACE,GOODSBARCODE,ENNAME,SUPPERITEM ,TOSTORE ORDER BY  B.STOREPLACE DESC  "

        End If
        Me.ProgressBar1.Value = 50
        Dtab = App.ReturnDataTable(sql)

        If Dtab.Rows.Count <= 0 Then
            MessageBox.Show("沒有數據可以導出！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Me.ProgressBar1.Value = 0
            Exit Sub

        End If
        ''導出撿貨清單

        Me.ProgressBar1.Value = 60
        Dim SelectCasioNo As New Frm_SelectCasioNo
        Dim CasioNo As String = String.Empty
        SelectCasioNo.ShowDialog()
        If SelectCasioNo.BtnYes.DialogResult <> DialogResult.OK Then
            Exit Sub
        End If
        If SelectCasioNo.CBCasioNO.Text = "" Then
            MessageBox.Show("請選擇Casio編號", " 提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.ProgressBar1.Value = 0
            Exit Sub
        Else
            CasioNo = SelectCasioNo.CBCasioNO.Text.ToString.Trim
            Me.ProgressBar1.Value = 90
        End If
        ''Dim DTTime As String = String.Empty
        ''DTTime = Now.ToString("yyMMddHHmmss")
        Me.ProgressBar1.Value = 100
        ExportToTxtFar(Dtab, "StorePlacePickUp-" & CasioNo & ".txt")
    End Sub

    Private Sub Frm_INTPickGoodsList_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Lod_CBStore()
        Loadcmb()
        Me.CBStore.SelectedIndex = -1
    End Sub
    Private Sub Loadcmb()
        Dim sql As String = String.Empty
        sql = "select distinct  convert(bit,0) as Isselected, isnull(fport,'') as Aisle from bsc_Storage order by Aisle ASC "
        Dim rst As New DataSet
        rst = ReturnDataset(sql)
        Me.Dgv_Port.DataSource = rst.Tables(0)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Dtab1 As DataTable
        Dim sql As String = ""
        Dim StartDate As String = ""
        Dim EndDate As String = ""
        StartDate = FormatDate(Me.StartDate.Value)
        EndDate = FormatDate(Me.EndDate.Value)
        Me.ProgressBar1.Value = 10
        Dim Porttable As DataTable = DirectCast(Me.Dgv_Port.DataSource, DataTable)
        Dim PortStr As Text.StringBuilder = New Text.StringBuilder
        Dim count As Int32 = 0
        If Porttable.Rows.Count > 0 Then
            For Each row As DataRow In Porttable.Rows
                If row("Isselected") = True Then
                    count = count + 1
                    If PortStr.Length > 0 Then
                        PortStr.Append(",")
                    End If
                    PortStr.Append("'")
                    PortStr.Append(row(1))
                    PortStr.Append("'")
                End If
            Next
        End If
        Me.ProgressBar1.Value = 20

        Dim Port As String = PortStr.ToString

        ''char(9)為Tab鍵，char(13)為Enter鍵
        sql = " SELECT D.FSHORTNAME,REPLACE(B.GOODSBARCODE,'*','') AS GOODSBARCODE,C.ENNAME,SUM(B.GOODSAMOUNT) AS GOODSAMOUNT,"
        sql += " C.SUPPERITEM FROM"
        sql += " DBO.STOR_TRADEMASTER A LEFT JOIN STOR_TRADEDETAIL B ON A.BONUM=B.BONUM LEFT JOIN BSC_GOODS C ON B.GOODSBARCODE =  "
        sql += " C.BARCODE LEFT JOIN BSC_STOREHOUSE D ON A.TOSTORE = D. FCODE RIGHT JOIN (SELECT LISTCODE AS INOROUTTYPE FROM "
        sql += " DBO.BSC_OTHERDEFINEB WHERE ITEMNO=(SELECT ID FROM DBO.BSC_OTHERDEFINEA WHERE ITEMCODE = 'OUTSTORETYPE')AND LISTC = "
        sql += "  'Y') F ON A.INOROUTTYPE = F.INOROUTTYPE WHERE TRADETYPE = '-' "

        sql += "and  Convert(varchar(10),a.DeliverDate,21) between '" & StartDate & "' and '" & EndDate & "'"



        If Me.CBStore.Text.Length > 0 Then
            sql += " and d.fshortName  = '" & Me.CBStore.Text.ToString.Trim & "'"
        End If

        sql += "group by GoodsBarCode,Enname,SupperItem ,d.fshortName order by d.fshortName desc "


        'If count > 0 Then
        '    sql = " select Distinct b.storeplace,Replace(b.goodsbarcode,'*','') as goodsbarcode,c.enname,c.SupperItem,sum(b.goodsamount) as goodsamount,e.fport from "
        '    sql += " dbo.Stor_TradeMaster a left join Stor_TradeDetail b on a.bonum=b.bonum "
        '    sql += "left join bsc_goods c on b.goodsbarcode = c.barcode left join BSC_StoreHouse d on a.tostore = d. fcode left join bsc_storage e on b.storeplace = e.fstorageid  where inorouttype = '03'  and TradeType = '-' "
        '    sql += "and  Convert(varchar(10),a.DeliverDate,21) between '" & StartDate & "' and '" & EndDate & "'"
        '    Me.ProgressBar1.Value = 70


        '    If Me.CBStore.Text.Length > 0 Then
        '        sql += " and d.fshortName  = '" & Me.CBStore.Text.ToString.Trim & "'"
        '    End If

        '    sql += "   and isnull(e.fport,'') in (" & Port & ")  group by e.fport, b.Storeplace,GoodsBarCode,Enname,SupperItem order by  b.Storeplace desc "
        'Else
        '    sql = " select Distinct b.storeplace,Replace(b.goodsbarcode,'*','') as goodsbarcode,c.enname,c.SupperItem,sum(b.goodsamount) as goodsamount,e.fport from "
        '    sql += " dbo.Stor_TradeMaster a left join Stor_TradeDetail b on a.bonum=b.bonum "
        '    sql += "left join bsc_goods c on b.goodsbarcode = c.barcode left join BSC_StoreHouse d on a.tostore = d. fcode left join bsc_storage e on b.storeplace = e.fstorageid  where inorouttype = '03'  and TradeType = '-' "
        '    sql += "and  Convert(varchar(10),a.DeliverDate,21) between '" & StartDate & "' and '" & EndDate & "'"
        '    Me.ProgressBar1.Value = 70


        '    If Me.CBStore.Text.Length > 0 Then
        '        sql += " and d.fshortName  = '" & Me.CBStore.Text.ToString.Trim & "'"
        '    End If

        '    sql += "  group by e.fport, b.Storeplace,GoodsBarCode,Enname,SupperItem order by  b.Storeplace desc "

        'End If
        Me.ProgressBar1.Value = 80
        Dtab1 = App.ReturnDataTable(sql)
        If Dtab1.Rows.Count <= 0 Then
            MessageBox.Show("沒有數據可以列印！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Me.ProgressBar1.Value = 0
            Exit Sub
        End If
        Me.ProgressBar1.Value = 100
        ExportToTxt(Dtab1, "分貨清單.txt")
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        Dim mTable As DataTable = DirectCast(Me.Dgv_Port.DataSource, DataTable)
        If Me.CheckBox1.Checked = True Then
            If mTable.Rows.Count > 0 Then
                For Each row As DataRow In mTable.Rows
                    row("Isselected") = True
                Next
            End If
        End If
        If Me.CheckBox1.Checked = False Then
            If mTable.Rows.Count > 0 Then
                For Each row As DataRow In mTable.Rows
                    row("Isselected") = False
                Next
            End If
        End If
    End Sub

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.ProgressBar1.Value = 0

        Dim mDs As DataSet

        Try

            Dim Porttable As DataTable = DirectCast(Me.Dgv_Port.DataSource, DataTable)
            Dim PortStr As Text.StringBuilder = New Text.StringBuilder
            Dim count As Int32 = 0
            If Porttable.Rows.Count > 0 Then
                For Each row As DataRow In Porttable.Rows
                    If row("Isselected") = True Then
                        count = count + 1
                        If PortStr.Length > 0 Then
                            PortStr.Append(",")
                        End If
                        PortStr.Append("'")
                        PortStr.Append(row(1))
                        PortStr.Append("'")
                    End If
                Next
            End If


            Dim Port As String = PortStr.ToString

            Dim Pathstr As String = ""
            Dim SubPathstr As String = ""
            Dim ConfigNode() As String = ReadConfigFile.Split(";")
            Dim mServer As String = ""
            Dim mDataBase As String = ""
            Dim mUserID As String = ""
            Dim mPassword As String = ""
            Dim sql As String = ""
            Dim InOrOut As String = ""
            Dim StartDate As String = ""
            Dim EndDate As String = ""
            Me.ProgressBar1.Value = 10

            'Pathstr = "D:\Reports\Store\RepInOutCount.rpt"
            Pathstr = GetReportPath("Store", "PickGoodsList1.rpt")
            Me.ProgressBar1.Value = 20
            For i As Integer = 0 To ConfigNode.Length - 1
                If ConfigNode(i).Split("=")(0).ToLower = "Server".ToLower Then
                    mServer = ConfigNode(i).Split("=")(1)
                End If
                If ConfigNode(i).Split("=")(0).ToLower = "DataBase".ToLower Then
                    mDataBase = ConfigNode(i).Split("=")(1)
                End If
                If ConfigNode(i).Split("=")(0).ToLower = "Uid".ToLower Then
                    mUserID = ConfigNode(i).Split("=")(1)
                End If
                If ConfigNode(i).Split("=")(0).ToLower = "Pwd".ToLower Then
                    mPassword = ConfigNode(i).Split("=")(1)
                End If
            Next
            Me.ProgressBar1.Value = 30


            Dim myReportDoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
            Dim myParameterValues As New CrystalDecisions.Shared.ParameterValues
            Dim myDiscreteValue As New CrystalDecisions.Shared.ParameterDiscreteValue
            Dim logonInfo As New CrystalDecisions.Shared.TableLogOnInfo
            Dim table As CrystalDecisions.CrystalReports.Engine.Table
            Me.ProgressBar1.Value = 40
            myReportDoc.Load(Pathstr)
            Me.ProgressBar1.Value = 50
            For Each table In myReportDoc.Database.Tables
                logonInfo = table.LogOnInfo
                With logonInfo.ConnectionInfo
                    .ServerName = mServer
                    .DatabaseName = mDataBase
                    .UserID = mUserID
                    .Password = mPassword
                End With
                table.ApplyLogOnInfo(logonInfo)
            Next

            Me.ProgressBar1.Value = 60

            StartDate = FormatDate(Me.StartDate.Value)
            EndDate = FormatDate(Me.EndDate.Value)

            If count > 0 Then
                sql = " SELECT  B.STOREPLACE,REPLACE(B.GOODSBARCODE,'*','') AS GOODSBARCODE,C.ENNAME,SUM(B.GOODSAMOUNT) AS GOODSAMOUNT,SUM(B.GOODSAMOUNT)* isnull(PackingNum,1) as PackingNum , " & vbCrLf
                sql += " C.SUPPERITEM,ISNULL(E.FPORT,'') AS FPORT  FROM  " & vbCrLf
                sql += " DBO.STOR_TRADEMASTER A LEFT JOIN STOR_TRADEDETAIL B ON A.BONUM=B.BONUM  " & vbCrLf
                sql += " LEFT JOIN BSC_GOODS C ON B.GOODSBARCODE = C.BARCODE " & vbCrLf
                sql += " LEFT JOIN BSC_STOREHOUSE D ON A.TOSTORE = D. FCODE LEFT JOIN BSC_STORAGE E ON B.STOREPLACE = E.FSTORAGEID " & vbCrLf
                sql += " RIGHT JOIN (SELECT LISTCODE AS INOROUTTYPE FROM DBO.BSC_OTHERDEFINEB WHERE ITEMNO=(SELECT ID FROM  " & vbCrLf
                sql += " DBO.BSC_OTHERDEFINEA WHERE ITEMCODE = 'OUTSTORETYPE')AND LISTC = 'Y') F ON A.INOROUTTYPE = F.INOROUTTYPE " & vbCrLf
                sql += " WHERE TRADETYPE = '-'  " & vbCrLf


                sql += "and  Convert(varchar(10),a.DeliverDate,21) between '" & StartDate & "' and '" & EndDate & "'"
                Me.ProgressBar1.Value = 70


                If Me.CBStore.Text.Length > 0 Then
                    sql += " and d.fshortName  = '" & Me.CBStore.Text.ToString.Trim & "'"
                End If

                sql += "   and isnull(e.fport,'') in (" & Port & ")  group by e.fport, b.Storeplace,GoodsBarCode,Enname,SupperItem,PackingNum order by  b.Storeplace desc "
            Else
                sql = " SELECT  B.STOREPLACE,REPLACE(B.GOODSBARCODE,'*','') AS GOODSBARCODE,C.ENNAME,SUM(B.GOODSAMOUNT) AS GOODSAMOUNT,SUM(B.GOODSAMOUNT)* isnull(PackingNum,1) as PackingNum , " & vbCrLf
                sql += " C.SUPPERITEM,ISNULL(E.FPORT,'') AS FPORT  FROM  " & vbCrLf
                sql += " DBO.STOR_TRADEMASTER A LEFT JOIN STOR_TRADEDETAIL B ON A.BONUM=B.BONUM  " & vbCrLf
                sql += " LEFT JOIN BSC_GOODS C ON B.GOODSBARCODE = C.BARCODE " & vbCrLf
                sql += " LEFT JOIN BSC_STOREHOUSE D ON A.TOSTORE = D. FCODE LEFT JOIN BSC_STORAGE E ON B.STOREPLACE = E.FSTORAGEID " & vbCrLf
                sql += " RIGHT JOIN (SELECT LISTCODE AS INOROUTTYPE FROM DBO.BSC_OTHERDEFINEB WHERE ITEMNO=(SELECT ID FROM  " & vbCrLf
                sql += " DBO.BSC_OTHERDEFINEA WHERE ITEMCODE = 'OUTSTORETYPE')AND LISTC = 'Y') F ON A.INOROUTTYPE = F.INOROUTTYPE " & vbCrLf
                sql += " WHERE TRADETYPE = '-'  " & vbCrLf


                sql += "and  Convert(varchar(10),a.DeliverDate,21) between '" & StartDate & "' and '" & EndDate & "'"
                Me.ProgressBar1.Value = 70


                If Me.CBStore.Text.Length > 0 Then
                    sql += " and d.fshortName  = '" & Me.CBStore.Text.ToString.Trim & "'"
                End If

                sql += "  group by e.fport, b.Storeplace,GoodsBarCode,Enname,SupperItem,PackingNum order by  b.Storeplace desc "

            End If

            Me.ProgressBar1.Value = 80
            mDs = New DataSet
            mDs = ReturnDataset(sql)
            If mDs.Tables(0).Rows.Count <= 0 Then
                MessageBox.Show("沒有數據可以列印！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            End If
            Me.ProgressBar1.Value = 90
            'myReportDoc.SetParameterValue("StartDate", StartDate)
            'myReportDoc.SetParameterValue("EndDate", EndDate)

            myReportDoc.SetDataSource(mDs.Tables(0))

            CrystalReportViewer1.ReportSource = myReportDoc
            Dim TxtStartDate As CrystalDecisions.CrystalReports.Engine.TextObject = myReportDoc.ReportDefinition.ReportObjects("Text7")
            Dim TxtEndDate As CrystalDecisions.CrystalReports.Engine.TextObject = myReportDoc.ReportDefinition.ReportObjects("Text8")
            TxtStartDate.Text = Format(Me.StartDate.Value.ToString("yyyy/MM/dd"), "yyyy/MM/dd")
            TxtEndDate.Text = Format(Me.EndDate.Value.ToString("yyyy/MM/dd"), "yyyy/MM/dd")

            CrystalReportViewer1.Zoom(1)
            Me.ProgressBar1.Value = 100
        Catch ex As Exception
        Finally
            mDs = Nothing
        End Try
    End Sub
End Class
